<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="elf" uri="/elf-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
request.setAttribute("comma",",");
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>main</title>
<jsp:include page="../head.jsp" />
<script type="text/javascript" src="<%=path %>/js/ueditor/editor_all_min.js"></script>
<script type="text/javascript" src="<%=path %>/js/ueditor/editor_config.js"></script>
<link rel="stylesheet" type="text/css" href="<%=path %>/js/ueditor/themes/default/ueditor.css" />
<script type="text/javascript">
	var totalEditor;
	$(function(){
		$("#checkAll").click(function () {
	            if ($(this).prop("checked") == true) {
	                $("input[name='checkbox']").each(function () {
	                    $(this).prop("checked", true);
	                });
	            } else {
	                $("input[name='checkbox']").each(function () {
	                    $(this).prop("checked", false);
	                });
	            }
	        });
		var editor = new baidu.editor.ui.Editor({
            allHtmlEnabled:true
    	});
		$("#menuOrder>:hidden:first").next().prop("selected","selected").attr("notChange","1");
		var option = $("#parentMenuId").change(function(){
			$("#btnSave").prop("disabled","disabled");
			$("#menuOrder").val("");
			$.ajax({
				url			:"<%=path%>/menu/menuAjax_loadMenu_menuListAction.action",
				type		:"POST",
				dataType	:"json",
				data		:{parentMenuId : $(this).val()},
				success		:function(data){
					var html = ""; 
					var selected = false;
					var find = false;
					if (data!=null && data.length>0) {
						//存在数据,加载数据
						for (var j = 0;j<data.length;j++) {
							//循环读取节点内容
							var menu=data[j];
							if(menu.menuId==${menu.menuId}){
								//匹配成功
								selected = true;
								find=true;
								continue;
							}
							html+=("<option value = '"+menu.backupData+"'"+(selected ? " selected='selected' notChange='1'":"")+">"+menu.menuName+"</option>");
							if(selected)
								selected = false;
						}
					}
					//加载空节点
					$("#menuOrder").html(html + "<option value = '' "+(selected ? " selected='selected' notChange='1'":(!find ? " selected='selected'":""))+"></option>");
					$("#btnSave").removeAttr("disabled");
				},
				error		:function(){
					$("#menuOrder").html("");
					j_tip("加载排序菜单失败.", 'error');
					$("#btnSave").removeAttr("disabled");
				}
			});	
		});
		editor.render('baiduEditor');
		totalEditor=editor;
	});
	function save(){
		if(trim($("#menuName").val())==""){
			addError("menuName","菜单名称不能为空.");
			return;
		}
		var contextIds = "";
        $("input[name='checkbox']:checked").each(function () {
            contextIds += $(this).val() + " ";
        });
    	 //转向当前页
        if (contextIds == "") {
        	addError("checkbox","必须给菜单分配权限！");
            return;
        }
    	if (!checkForm(document.getElementById("saveForm"))) {
			return;
		}
    	var selectValue = $("#menuOrder>[value='"+$("#menuOrder").val()+"']:first")
    	if(selectValue.attr("source")){
    		$("#afterMenuId").val(selectValue.attr("source"));
    	}else{
    		$("#afterMenuId").val("");
    	}
    	//判断排序是否发生变更
    	if(selectValue.attr("notChange")){
    		//没变更，还原成之前的
    		$("#hidOrder").val("${menu.backupData}");
    	}else{
    		$("#hidOrder").val($("#menuOrder").val());
    	}
    	xbox.load("loading");
        $("#authorityString").val($.trim(contextIds));
        $("#remark").val(totalEditor.getContent());
		$("#saveForm").ajaxSubmit(function(){
	    	$("#refreshForm").submit();
	    });
	}
	function turnBack(){
	   $("#refreshForm").submit();
	}

</script>

</head>
<body>
<form action="<%=path%>/menu/menu_modify_listAction.action" method="post" id="saveForm">
<table width="98%" align="center" border="0" cellpadding="4" cellspacing="1" bgcolor="#F1F1F1" style="margin-bottom:8px">
  <tr bgcolor="#FFFFFF">
    <td colspan="4" height="16"><span><input type="hidden" id="afterMenuId" name="menu.afterMenuId"/></span></span></td>
  </tr>
  <tr bgcolor="#FFFFFF">
    <td width="10%" align="right" bgcolor="#FFFFFF"><span style="color:red;">*</span>菜单名称：</td>
    <td width="40%" align="left" bgcolor="#FFFFFF">
              <input maxlength="25" type="text" name="menu.menuName" id="menuName" value="${menu.menuName}" />
    </td>
    <td width="10%" align="right" bgcolor="#FFFFFF">菜单编号：</td>
    <td width="40%" align="left" bgcolor="#FFFFFF">
             <input maxlength="25" type="text" name="menu.menuNo" id="menuNo" value="${menu.menuNo}" />
    </td>
  </tr>
  <tr bgcolor="#FFFFFF" <c:if test="${DEVELOP_MODEL ne true}">style="display:none;"</c:if>>
   	<td width="10%" align="right" bgcolor="#FFFFFF">所属公司：</td>
    <td width="40%" align="left" bgcolor="#FFFFFF">
   	<select id="parentCompanyId" name="companyId">
    	<option value="1">默认设置</option>
    	<c:forEach items="${companyList}" var="com">
    	<option value="${com.companyId}"><c:out value="${com.companyName}" /></option>
    	</c:forEach>
   	</select>
    </td>
    <td width="10%" align="right" bgcolor="#FFFFFF">所属部门：</td>
    <td width="40%" align="left" bgcolor="#FFFFFF">
   	<select id="parentDepartmentId" name="departmentId">
    	<option value="1">默认设置</option>
    	<c:forEach items="${departmentList}" var="dep">
    	<option value="${dep.departmentId}"><c:out value="${dep.departmentName}" /></option>
    	</c:forEach>
   	</select>
    </td>
    
   </tr>
  
   <tr bgcolor="#FFFFFF" <c:if test="${DEVELOP_MODEL ne true}">style="display:none;"</c:if>>
    <td width="10%" align="right" bgcolor="#FFFFFF">上级菜单：</td>
    <td width="40%" align="left" bgcolor="#FFFFFF">
    <select id="parentMenuId" name="menu.parentMenuId">
    	<option value="0">&nbsp;&nbsp;&nbsp;&nbsp;</option>
    	<c:forEach items="${menuList}" var="m">
    	<c:if test="${m.parentMenuId eq 0 and m.menuId ne menu.menuId}">
    		<option value="${m.menuId}" <c:if test="${menu.parentMenuId eq m.menuId}">selected="selected"</c:if>><c:out value="${m.menuName}" /></option>
    	</c:if>
    	</c:forEach>
    </select>
    </td>
   	<td width="10%" align="right" bgcolor="#FFFFFF">位于：</td>
    <td width="40%"  align="left" bgcolor="#FFFFFF">
    	<select id="menuOrder">
    		<c:forEach items="${menuList}" var="m">
		    	<c:if test="${m.parentMenuId eq menu.parentMenuId }">
		    		<option value="${m.backupData}" source="${m.menuId }" <c:if test="${m.menuId eq menu.menuId}">style="display:none;"</c:if>><c:out value="${m.menuName}" /></option>
		    	</c:if>
	    	</c:forEach>
	    	<option value=""></option>
	    </select>之前
    </td>
  </tr>
  
  <tr bgcolor="#FFFFFF" <c:if test="${DEVELOP_MODEL ne true}">style="display:none;"</c:if>>
  	<td width="10%" align="right" bgcolor="#FFFFFF">菜单URL：</td>
    <td width="40%" align="left" bgcolor="#FFFFFF">
             <input type="text" name="menu.menuUrl" id="menuUrl" value="${menu.menuUrl}"/>
    </td>
    <td width="10%" align="right" bgcolor="#FFFFFF">菜单级别：</td>
    <td width="40%" align="left" bgcolor="#FFFFFF"><input type="radio" name="menu.publicMenu" value="1" id="publicMenuYes" <c:if test="${menu.publicMenu eq 1}">checked="checked"</c:if>/>公共菜单&nbsp;<input type="radio" value="0" name="menu.publicMenu" id="publicMenuNo" <c:if test="${menu.publicMenu ne 1}">checked="checked"</c:if> />普通菜单</td>
  </tr>
  
  <tr bgcolor="#FFFFFF" <c:if test="${DEVELOP_MODEL ne true}">style="display:none;"</c:if>>
    <td width="10%" align="right" bgcolor="#FFFFFF">菜单描述：</td>
    <td width="40%" align="left" bgcolor="#FFFFFF">
             <input type="text" name="menu.menuDesc" id="menuDesc" value="${menu.menuDesc}" />
    </td>
    <td width="10%" align="right" bgcolor="#FFFFFF">是否启用：</td>
    <td width="40%" align="left" bgcolor="#FFFFFF"><input type="radio" name="menu.openFlag" value="1" id="openFlagYes" <c:if test="${menu.openFlag eq 1}">checked="checked"</c:if> />启用&nbsp;<input type="radio" value="0" name="menu.openFlag" id="openFlagNo" <c:if test="${menu.openFlag eq 0}">checked="checked"</c:if> />禁用</td>
  </tr>
  <tr bgcolor="#FFFFFF" <c:if test="${DEVELOP_MODEL ne true}">style="display:none;"</c:if>>
    <td width="10%" align="right" bgcolor="#FFFFFF">菜单图标：</td>
    <td width="90%" colspan="3" align="left" bgcolor="#FFFFFF">
           <input type="text" name="menu.menuIcon" id="menuIcon" value="${menu.menuIcon}" />
    </td>
  </tr>
    <tr bgcolor="#FFFFFF" <c:if test="${DEVELOP_MODEL ne true}">style="display:none;"</c:if>>
    <td width="10%" align="right" bgcolor="#FFFFFF">菜单权限：</td>
    <td width="90%" colspan="3" align="left" bgcolor="#FFFFFF">
            <input type="checkbox" id="checkAll" name="checkAll"/>全选
             <c:forEach items="${authorityList}" var="auth">
		    		<input type="checkbox" name="checkbox" id="checkbox" value="${auth.authorityId}" <c:if test="${fn:contains(authorityString,elf:appendKick(auth.authorityId,comma))}">checked="checked"</c:if> />${auth.authorityName}
		    	</c:forEach>
    </td>
  </tr>
  <tr bgcolor="#FFFFFF">
    <td width="10%" align="right" bgcolor="#FFFFFF" valign="top">帮助：</td>
    <td width="90%" colspan="3" align="left" bgcolor="#FFFFFF">
              <textarea rows="3"  name="baiduEditor" id="baiduEditor"><c:out value="${menu.remark}" /></textarea>
    </td>
  </tr>
  <tr align="right" bgcolor="#F1F1F1">
	<td height="36" colspan="4" align="center">
		<input type='button' class="btn  btn-primary" id="btnSave" onclick="save();" value='保存' />
	    &nbsp;<input type='button' class="btn" onclick="turnBack();" value='返回' />
</td>
</tr>
</table>
<input type="hidden" id="hidOrder" name="menu.menuOrder" />
<input type="hidden" id="remark" name="menu.remark" />
<input type="hidden" name="menu.menuId" value="${menu.menuId}"/>
<input type="hidden" name="menu.createTime" value="${menu.createTime}"/>
<input type="hidden" name="menu.deleteFlag" value="${menu.deleteFlag}"/>
<input type="hidden" id="backupData" name="menu.backupData" value="${menu.backupData}"/>
<input type="hidden" name="oldParentMenuId" value="${menu.parentMenuId}"/>
<input type="hidden" id="authorityString" name="authorityString" value="${authorityString}" />
</form>

<form action="${listUrl}" method="post" style="display: none;" id="refreshForm" name="refreshForm"/>


</body>
</html>
